package com.bitspilani.infonet.serviceinterface;

import android.os.AsyncTask;
import android.widget.Toast;

import com.bitspilani.infonet.MainActivity;
import com.bitspilani.infonet.bo.LastKnownLocation;
import com.bitspilani.infonet.bo.RiskLocationDetails;
import com.bitspilani.infonet.bo.RiskResponse;
import com.bitspilani.infonet.bo.StorageResponse;
import com.bitspilani.infonet.constants.GlobalConstants;
import com.bitspilani.infonet.core.LocationProvider;
import com.bitspilani.infonet.serviceinterface.ServiceInvoker;
import com.google.gson.Gson;

public class ServiceTask extends AsyncTask<String, Integer, String[]>{
private Gson gson = new Gson();
//private MainActivity mainActivityRef = new MainActivity();
	@Override
	protected String[] doInBackground(String... params) {
		
		MainActivity.mainActivityRef.showSpinner();
		
		String url = params[0];
		String jsonPayload = params[1];
		try {
			String[] res = new String[2];
			res[0] = url;
			res[1] = ServiceInvoker.invokeServie(url, jsonPayload);
			return res;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	  protected void onPostExecute(String[] result) {
		  System.out.println("result: "+result[1]+" URL: "+result[0]);
		  String url = result[0];
		  String serviceRes = result[1];
		  if(url.equalsIgnoreCase(GlobalConstants.STORE_PATCH_DETAILS)){
			  StorageResponse response = gson.fromJson(serviceRes, StorageResponse.class);
			  if(response.getStatus()!= null && response.getStatus().equalsIgnoreCase("success")){
				  MainActivity.mainActivityRef.hideSpinnerAndShowToast("Patch Details stored", Toast.LENGTH_SHORT );
			  }else{
				  MainActivity.mainActivityRef.hideSpinnerAndShowToast("STORE_PATCH_DETAILS ERROR", Toast.LENGTH_SHORT );
				  System.out.println();
			  }
			  
			  
		  }else if(url.equalsIgnoreCase(GlobalConstants.STORE_RISK_LOCATION)){
			  StorageResponse response = gson.fromJson(serviceRes, StorageResponse.class);
			  if(response.getStatus()!= null && response.getStatus().equalsIgnoreCase("success")){
				  MainActivity.mainActivityRef.hideSpinnerAndShowToast("Risk Location stored", Toast.LENGTH_SHORT );
			  }else{
				  MainActivity.mainActivityRef.hideSpinnerAndShowToast("STORE_RISK_LOCATION ERROR", Toast.LENGTH_SHORT );
				  System.out.println();
			  }
			  
		  }else if(url.equalsIgnoreCase(GlobalConstants.GET_SPEED_LIMIT_RISK)){
			  RiskResponse riskResponse = gson.fromJson(serviceRes, RiskResponse.class);
			  
			  if(riskResponse.getStatus()!= null && riskResponse.getStatus().equalsIgnoreCase("failure")){
				  MainActivity.mainActivityRef.hideSpinnerAndShowToast("GET_SPEED_LIMIT_RISK ERROR", Toast.LENGTH_SHORT );
			  }
			  
			  if(riskResponse.isRiskExists()){
				  MainActivity.vibrate();
				  MainActivity.mainActivityRef.hideSpinnerAndShowToast("Risk Found!", Toast.LENGTH_SHORT );
				 MainActivity.mainActivityRef.showRiskNotification(riskResponse);
			  }else{
				  MainActivity.mainActivityRef.hideSpinnerAndShowToast("No Risk found", Toast.LENGTH_SHORT );
				  System.out.println();
			  }
			  
			  if(riskResponse.getSpeedLimit() != 0f){
				  LastKnownLocation loc = LocationProvider.getBestKnownLocation();
				  
				  if(loc.getSpeed() * 3.6 > riskResponse.getSpeedLimit()){
					  MainActivity.vibrate();
					  MainActivity.mainActivityRef.showOverSpeedingNotification( riskResponse.getSpeedLimit(), loc.getSpeed());
				 }
				  
				  
			  }
		  }else{
			  throw new RuntimeException("Unable to handle response for URL: "+url);//break the app
		  }
		  
		  
	     }

	  
	  
}
